<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="700px"
    @close="close"
  >
    <el-form ref="formRef" label-width="140px" :model="form" :rules="rules">
      <el-form-item label="客户全称" prop="CustomerAllName">
        <el-input
          v-model="form.CustomerAllName"
          :disabled="customerMod"
          @input="doesExist"
        />
      </el-form-item>
      <el-form-item label="所属区域" prop="title">
        <el-cascader
          v-model="form.City"
          :options="cityOptions"
          placeholder="选择地区"
          :props="CityProps"
          style="width: 300px"
        />
      </el-form-item>
      <el-form-item label="所属行业" prop="title">
        <el-cascader
          v-model="form.Industry"
          :options="industryOptions"
          placeholder="选择行业"
          filterable
          filter-placeholder=""
          :props="IndustryProps"
          style="width: 200px"
        />
      </el-form-item>
      <el-form-item label="地址" prop="Address">
        <el-input v-model="form.Address" />
      </el-form-item>
      <el-form-item label="电话" prop="Telphone">
        <el-input v-model="form.Telphone" />
      </el-form-item>
      <el-form-item label="法定代表人" prop="LegalPerson">
        <el-input v-model="form.LegalPerson" />
      </el-form-item>
      <el-form-item label="客户负责人电话" prop="LegalMobile">
        <el-input v-model="form.LegalMobile" />
      </el-form-item>
      <el-form-item label="业务联系人及职务" prop="BusinessPerson">
        <el-input v-model="form.BusinessPerson" />
      </el-form-item>
      <el-form-item label="业务联系人电话" prop="BusinessPersonMobile">
        <el-input v-model="form.BusinessPersonMobile" />
      </el-form-item>
      <el-form-item label="主营产品" prop="SalesProducts">
        <el-input v-model="form.SalesProducts" />
      </el-form-item>
      <el-form-item label="主要销售区域" prop="title">
        <el-input v-model="form.SalesArea" />
      </el-form-item>
      <el-form-item label="主要客户群体" prop="CustomerGroup">
        <el-input v-model="form.CustomerGroup" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @dblclick.prevent @click="save">
        保 存
      </el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { EditCustomer } from '@/api/customer'
  import { CitySelect, IndustrySelect } from '@/api/common'

  export default defineComponent({
    name: 'Customer',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        formRef: null,
        form: {},
        rules: {
          // title: [{ required: true, trigger: 'blur', message: '请输入标题' }],
        },
        title: '',
        dialogFormVisible: false,
        cityOptions: [],
        industryOptions: [],
        actstaffpostid: 0,
        customerMod: 1,
        IndustryProps: {
          expandTrigger: 'hover',
        },
        CityProps: {
          expandTrigger: 'hover',
        },
      })
      const doesExist = async (val) => {
        state.form.CustomerAllName = val.replace(
          /[^\u4e00-\u9fa5a-zA-Z0-9]/g,
          ''
        )
      }
      const showEdit = (row) => {
        state.title = '客户信息维护'
        state.form = Object.assign({}, row)
        state.dialogFormVisible = true
      }
      const close = () => {
        state['formRef'].resetFields()
        state.form = {
          roles: [],
        }
        state.dialogFormVisible = false
      }
      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { msg } = await EditCustomer(state.form)
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            emit('fetch-data')
            close()
          }
        })
      }

      const fetchData = async () => {
        state.listLoading = true
        const {
          data: { cityOptions },
        } = await CitySelect()
        const {
          data: { industryOptions },
        } = await IndustrySelect()
        state.cityOptions = cityOptions
        state.industryOptions = industryOptions
        state.listLoading = false
      }
      onMounted(() => {
        const userInfo = JSON.parse(localStorage.getItem('userInfo'))
        const actstaffpost = JSON.parse(userInfo.StaffPostID)
        state.actstaffpostid = actstaffpost[0][1]
        console.log('state.actstaffpostid', state.actstaffpostid)
        if (state.actstaffpostid === 9) {
          state.customerMod = 0
          console.log('state.customerMod', state.customerMod)
        }
        fetchData()
      })

      return {
        ...toRefs(state),
        showEdit,
        close,
        save,
        doesExist,
      }
    },
  })
</script>
